/**
 * 密码强度验证
 * 纯数字、纯字母、纯特殊符号 ---->  弱
 * 其中两种 -----> 中
 * 全占 -----> 强
 */
 var password = $(".form-item input[type=password]");
password.keyup(function(){
    var regxs = [];
    regxs[0] = /[^a-zA-Z0-9]/g; //特殊
    regxs[1] = /[a-zA-Z]/g; //字母
    regxs[2] = /[0-9]/g; //数字
    var str = password.val();
    var len = str.length;
    var sec = 0; //强度
    if (len >= 6) {
        for (var i = 0; i < regxs.length; i++) {
            if (str.match(regxs[i])) {
                sec++;
            }
        }
    }
    if(len > 0){
        $(".safe_window").css("display","block");
    }
    if(sec == 1 || len < 6){
        $(".safe_window .low").css("display","block");
        $(".safe_window .middle").css("display","none");
        $(".safe_window .safe").css("display","none");
        $(".safe_window span").html("弱");
        $(".safe_window span").css("color","#bc0001");
    }
    if(sec == 2){
        $(".safe_window .low").css("display","block");
        $(".safe_window .middle").css("display","block");
        $(".safe_window .safe").css("display","none");
        $(".safe_window span").html("中");
        $(".safe_window span").css("color","#ffd800");
    }
    if(sec == 3){
        $(".safe_window .low").css("display","block");
        $(".safe_window .middle").css("display","block");
        $(".safe_window .safe").css("display","block");
        $(".safe_window span").html("安全");
        $(".safe_window span").css("color","#49c519");
    }
});

$(".checkbox").click(function(){
    disableBtn();
});
$(".readme label").click(function(){
    disableBtn();
});

/**
 * 验证表单必填字段是否为空，必须要全部填写才能点击提交
 * @returns
 */
function disableBtn(){
    if($("#checkbox").is(":checked") && 
    	$("#nickName").val() != "" && 
    	$("#username").val() != "" && 
    	$("#password").val() != "" && 
    	$("#phoneNum").val() != "" && 
    	$("#verify").val() != ""){
        $(".fill").attr("disabled",false);
    }else{
        $(".fill").attr("disabled",true);
    }
}

var allNav = $(".header .nav .navbar li a");
for (var i = 0; i < allNav.length; i++) {
    allNav[i].onclick = function(){
        //清除之前的.avtive
        $(".navbar .active").attr("class","");
        //给当前的按钮加上.avtive
        this.className = "active";
    }
}

var times = 0;
/**
 * 更换验证码图片
 * @returns
 * 		void
 */
function changeImg(){
	console.log(1);
    var time = new Date().getTime();
    var s = time - times;
    console.log(s);
    if(s < 1000){
        alert("您的手速太快啦，请等一下再点吧");
        return ;
    }
    $("#verifyImg").attr("src","verify.jpg?"+time);
    times = time;
}

/**
 * 该函数可以向指定的输入框后面放入错误信息
 * @param selector 选择器
 * @param str 错误信息
 * @returns
 * 		void
 */
function checkInputNotNull(selector,str,flag){
	if($(selector).val() == "" || flag){
		$(selector).next(".error").html(str);
		$(selector).parent().attr("class","form-item haserror");
	}else{
		$(selector).next(".error").html("");
		$(selector).parent().attr("class","form-item");
	}
	//验证表单元素是否为空
	disableBtn();
}

/**
 * 验证表单字段是非符合规范
 * @returns
 */
function checkForm(){
	//用户名不能为中文字符和特殊符号
	var reg = /^(\d+[a-zA-Z]+|[a-zA-Z]+\d+)([0-9a-zA-Z]*)$/;
	var username = $("#username").val();
    if(username.length < 8 || username.length > 16){
    	checkInputNotNull($("#username"),"用户名必须是8-16个字符",true);
        return false;
    }else if(!reg.test(username)){
    	checkInputNotNull($("#username"),"用户名必须包含英文和数字，不能含有中文和特殊字符",true);
        return false;
    }
    //验证密码
    var password = $("#password").val();
    if(password.length < 6 || username.length > 16){
    	checkInputNotNull($("#password"),"密码必须是6-16个字符",true);
        return false;
    }
	//验证手机号
    var phoneRegex = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
    if(!phoneRegex.test($("#phoneNum").val())) {
    	checkInputNotNull($("#phoneNum"),"手机号输入有误",true);
        return false;
    }
	return true;
}

$(document).ready(function(){
	var errorBoxs = $(".error");
    for (var i = 0; i < errorBoxs.length; i++) {
    	if($(errorBoxs[i]).html() != ""){
    		$(errorBoxs[i]).css("display","block")
    	}
		var state = $(errorBoxs[i]).css("display");
		if(state == "block"){
			$(errorBoxs[i]).parent().attr("class","form-item haserror");
		}else{
			$(errorBoxs[i]).parent().attr("class","form-item");
		}
	}
});



